/**
 * 初始化学生详情对话框
 */
var StudentInfoDlg = {
    studentInfoData : {}
};

/**
 * 清除数据
 */
StudentInfoDlg.clearData = function() {
    this.studentInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
StudentInfoDlg.set = function(key, val) {
    this.studentInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
StudentInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
StudentInfoDlg.close = function() {
    parent.layer.close(window.parent.Student.layerIndex);
}

var instance1,instance2,instance3;

/**
 * 点击年级input框时
 */
StudentInfoDlg.onClickTerm = function (e, treeId, treeNode) {
    $("#termSel").attr("value", instance1.getSelectedVal());
    $("#termId").attr("value", treeNode.id);
};
/**
 * 点击班级input框时
 */
StudentInfoDlg.onClickClazz = function (e, treeId, treeNode) {
    $("#clazzSel").attr("value", instance2.getSelectedVal());
    $("#clazzId").attr("value", treeNode.id);
};
/**
 * 点击专业input框时
 */
StudentInfoDlg.onClickMajor = function (e, treeId, treeNode) {
    $("#majorSel").attr("value", instance3.getSelectedVal());
    $("#majorId").attr("value", treeNode.id);
};


/**
 * 显示年级选择的树
 */
StudentInfoDlg.showTermSelectTree = function () {
    var cityObj = $("#termSel");
    var cityOffset = $("#termSel").offset();
    $("#menuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};
/**
 * 显示年级选择的树
 */
StudentInfoDlg.showClazzSelectTree = function () {
    var cityObj = $("#clazzSel");
    var cityOffset = $("#clazzSel").offset();
    $("#menuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};
/**
 * 显示年级选择的树
 */
StudentInfoDlg.showMajorSelectTree = function () {
    var cityObj = $("#majorSel");
    var cityOffset = $("#majorSel").offset();
    $("#menuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};
/**
 * 隐藏年级选择的树
 */
StudentInfoDlg.hideSelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
        event.target).parents("#menuContent").length > 0)) {
        StudentInfoDlg.hideSelectTree();
    }
}

/**
 * 收集数据
 */
StudentInfoDlg.collectData = function() {
    this.set('id');
}

/**
 * 提交添加
 */
StudentInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/student/add", function(data){
        Feng.success("添加成功!");
        window.parent.Student.table.refresh();
        StudentInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.studentInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
StudentInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/student/update", function(data){
        Feng.success("修改成功!");
        window.parent.Student.table.refresh();
        StudentInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.studentInfoData);
    ajax.start();
}

$(function() {

    Feng.initValidator("userInfoForm", StudentInfoDlg.validateFields);

    var ztree1 = new $ZTree("treeDemo1", "/term/tree");
    ztree1.bindOnClick(StudentInfoDlg.onClickTerm);
    ztree1.init();
    instance1 = ztree1;

    var ztree2 = new $ZTree("treeDemo2", "/clazz/tree");
    ztree2.bindOnClick(StudentInfoDlg.onClickClazz);
    ztree2.init();
    instance2 = ztree2;

    var ztree3 = new $ZTree("treeDemo3", "/major/tree");
    ztree3.bindOnClick(StudentInfoDlg.onClickMajor);
    ztree3.init();
    instance3 = ztree3;

});
